package ca.seia.comp6521.a1.io;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.List;

import ca.seia.comp6521.a1.externalmergesort.ExternalMergeSortExecutionController;

public class DataFileWriter {

	private String appendModeFilePath;
	private boolean isFirstWrite = true;
	private BufferedWriter bufferedWriter;
	
	
	public DataFileWriter() {
	}
	
	public DataFileWriter(String appendModeFilePath) throws IOException {
		this(appendModeFilePath, createBufferedWriter(appendModeFilePath, false));
	}


	public DataFileWriter(String appendModeFilePath,
			BufferedWriter bufferedWriter) {
		this.appendModeFilePath = appendModeFilePath;
		this.bufferedWriter = bufferedWriter;
	}

	
	public void writeNextIntegers(int[] subList) throws IOException {
		setWriteMode();
		try {
			for (int i: subList){
				if(i==ExternalMergeSortExecutionController.UNUSED_NUMBER)
					continue;
				this.bufferedWriter.write(Integer.toString(i));
				this.bufferedWriter.newLine();
			}
		} finally {
			this.bufferedWriter.close();
		}
	}
	
	public void writeNextIntegers(int totalToWrite,int[] subList) throws IOException {
		setWriteMode();
		int countWritten = 0;
		try {
			for (int i: subList){
				if(countWritten  == totalToWrite )
					break;
				
				if(i==ExternalMergeSortExecutionController.UNUSED_NUMBER)
					continue;
				this.bufferedWriter.write(Integer.toString(i));
				this.bufferedWriter.newLine();
				countWritten ++;
			}
		} finally {
			this.bufferedWriter.close();
		}
	}
	

	public void writeNextInegers(Integer[] subList) throws IOException {
		setWriteMode();
		try {
			for (Integer i: subList){
				if(i==ExternalMergeSortExecutionController.UNUSED_NUMBER)
					continue;
				this.bufferedWriter.write(Integer.toString(i));
				this.bufferedWriter.newLine();
			}
		} finally {
			this.bufferedWriter.close();
		}
	}
	
	// used within the OutputBufferWriter.
	public void writeNextIntegers(List<Integer> subList) throws IOException {
		setWriteMode();
		try {
			for (Integer i: subList){
				if(i==ExternalMergeSortExecutionController.UNUSED_NUMBER)
					continue;
				this.bufferedWriter.write(Integer.toString(i));
				this.bufferedWriter.newLine();
			}
		} finally {
			this.bufferedWriter.close();
		}
	}
	
	/**
	 * 
	 * @param filePath
	 * @param subList
	 * @throws IOException
	 */
	public void writeFile(String filePath,Integer[] subList) throws IOException {
		Writer writer = new FileWriter(filePath);
		try(BufferedWriter bw = new BufferedWriter(writer)) {
			for (Integer i: subList){
				if(i==-1)
					continue;
				bw.write(Integer.toString(i));
				bw.newLine();
			}
		}
	}
	
	
	/**
	 * 
	 * @param filePath
	 * @param subList
	 * @throws IOException
	 */
	public void writeFile(String filePath,int[] subList) throws IOException {
		Writer writer = new FileWriter(filePath);
		try(BufferedWriter bw = new BufferedWriter(writer)) {
			for (int i: subList){
				if(i==ExternalMergeSortExecutionController.UNUSED_NUMBER)
					continue;
				bw.write(Integer.toString(i));
				bw.newLine();
			}
		}
	}
	
	public void setCreationMode() throws IOException {
		this.isFirstWrite = true;
		this.bufferedWriter = createBufferedWriter(appendModeFilePath, false);
	}
	
	public void setAppendMode() {
		this.isFirstWrite = false;
	}
	
	private void setWriteMode() throws IOException {
		if( isFirstWrite ) {
			this.isFirstWrite = false;
		} else {
			this.bufferedWriter = createBufferedWriter(appendModeFilePath, true);
		}
	}
	
	private static BufferedWriter createBufferedWriter(String filePath,boolean append) throws IOException {
		return new BufferedWriter(new FileWriter(filePath,append));
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
